﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace SqlServerTools
{
    static class  PubStCls
    {
        
        public static bool SavaFile(string fileContent, string filePath, string fileName)
        {
            //filePath = HttpContext.Current.Server.MapPath(filePath);
            if (filePath.EndsWith("\\") == false) { filePath = filePath + "/"; }
            using (FileStream fileStream = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
                    streamWriter.WriteLine(fileContent);
                    streamWriter.Flush();
                    streamWriter.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        //将字段类型转换为C#类型 
        //public static string ChangeToCSharpType(String strType)
        //{
        //    return  ChangeToCSharpType(strType,0,0);
        //}

        public static string ChangeToCSharpType(String strType,int intLength,int intDecLength)
        {
            string reval;

            switch (strType.ToLower())
            {
                case "int":
                case "smallint":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "String";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "System.Byte";
                    break;
                case "bit":
                    reval = "Boolean";
                    break;
                case "char":
                    reval = "String";
                    break;
                case "datetime":
                case "date":
                    reval = "System.DateTime";
                    break;
                case "decimal":
                    reval = "System.Decimal";
                    break;
                case "float":
                    reval = "System.Double";
                    break;
                case "image":
                    reval = "System.Byte";
                    break;
                case "money":
                    reval = "System.Decimal";
                    break;
                case "nchar":
                    reval = "String";
                    break;
                case "ntext":
                    reval = "String";
                    break;
                case "numeric":
                    if (intDecLength > 4)
                    { reval = "System.Double";  }
                    else
                    { reval = "System.Decimal"; }
                    break;
                case "nvarchar":
                    reval = "String";
                    break;
                case "real":
                    reval = "System.Single";
                    break;
                case "smalldatetime":
                    reval = "System.DateTime";
                    break;
                //case "smallint":
                //    reval = "Int16";
                //    break;
                case "smallmoney":
                    reval = "System.Decimal";
                    break;
                case "timestamp":
                    reval = "System.DateTime";
                    break;
                case "tinyint":
                    reval = "System.Byte";
                    break;
                case "uniqueidentifier":
                    reval = "System.Guid";
                    break;
                case "varbinary":

                    reval = "System.Byte";
                    break;
                case "varchar":
                    reval = "String";
                    break;
                case "variant":
                    reval = "Object";
                    break;
                default:
                    reval = "String";
                    break;
            }
            if (reval.ToUpper().StartsWith("System.".ToUpper()) == false) { reval = "System." + reval; }
            return reval;
        }

         /// <summary>跟据FieldTpye,生成SQL时跟据字段类型暂时存放：'',#'</summary>
        /// <param name="FieldType"></param>
        /// <returns></returns>
        public static String GetCabinet(String FieldType = "")
        {//跟据FieldTpye,生成SQL时跟据字段类型暂时存放：'',#'
            String StrReturn = "";
            switch (FieldType.ToUpper())
            {
                //字符、日期
                case "SAMLLDATETIME":
                case "DATETIME":
                case "CHAR":
                case "NCHAR":
                case "VARCHAR":
                case "NVARCHAR":
                case "TEXT":
                case "NTEXT":

                    StrReturn = "'";
                    break;
                //数值型
                case "NUMERIC":
                case "MONEY":
                case "INT":
                case "BIGINT":
                case "SMALLMONEY":
                    StrReturn = "";
                    break;
                default://其它类型一律视为字符串
                    StrReturn = "'";
                    break;
            }
            return StrReturn;
        }

        //根据类型名获得Type
        public static Type GetTypeByString(string TypeName)
        {
            if (TypeName.ToUpper().StartsWith("SYSTEM.") == true) { TypeName = TypeName.Substring("system.".Length, TypeName.Length - "system.".Length); }
            switch (TypeName.ToLower())
            {
                case "bool":
                    return Type.GetType("System.Boolean", true, true);
                case "byte":
                    return Type.GetType("System.Byte", true, true);
                case "sbyte":
                    return Type.GetType("System.SByte", true, true);
                case "char":
                    return Type.GetType("System.Char", true, true);
                case "decimal":
                    return Type.GetType("System.Decimal", true, true);
                case "double":
                    return Type.GetType("System.Double", true, true);
                case "float":
                    return Type.GetType("System.Single", true, true);
                case "int":
                    return Type.GetType("System.Int32", true, true);
                case "uint":
                    return Type.GetType("System.UInt32", true, true);
                case "long":
                    return Type.GetType("System.Int64", true, true);
                case "ulong":
                    return Type.GetType("System.UInt64", true, true);
                case "object":
                    return Type.GetType("System.Object", true, true);
                case "short":
                    return Type.GetType("System.Int16", true, true);
                case "ushort":
                    return Type.GetType("System.UInt16", true, true);
                case "string":
                    return Type.GetType("System.String", true, true);
                case "date":
                case "datetime":
                    return Type.GetType("System.DateTime", true, true);
                case "guid":
                    return Type.GetType("System.Guid", true, true);
                default:
                    return Type.GetType(TypeName, true, true);
            }
        }


        public static String GetDefultValue(String FieldType = "")
        //public static T GetDefultValue<T>(String FieldType = "")

        {
            String serReValue = "";
            switch (FieldType.ToUpper())
            {
                //字符、日期
                case "SAMLLDATETIME":
                case "DATETIME":
                case "DATE":
                    serReValue = "DateTime.Now";
                    break;
                case "CHAR":
                case "NCHAR":
                case "VARCHAR":
                case "NVARCHAR":
                case "TEXT":
                case "NTEXT":
                    serReValue = "\"\"";
                    break;
                //数值型
                case "NUMERIC":
                case "MONEY":
                case "INT":
                case "BIGINT":
                case "SMALLINT":
                case "SMALLMONEY":                
                    serReValue = "0";
                    break;
                default://其它类型一律视为字符串
                    serReValue = "";
                    break;
            }
            return serReValue;
        }
    }
}
